﻿using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiMinCRMPlugin
{
    /// <summary>
    /// 应收票据操作插件
    /// </summary>
    /// <remarks>
    /// 功能：应收票据在审核和删除后判断上游是否关联待认领收款清单，对待认领收款的已认领状态进行更新。
    /// 创建人：wzx
    /// 创建日期：2024.6.28
    /// 修改信息（1）：修改人，修改日期，修改内容
    /// </remarks>
    /// 
    [Description("应收票据审核删除更新待认领收款")]
    [HotUpdate]
    public class BillReceiveableUpdatePendingCollectionList: AbstractOperationServicePlugIn
    {
        //OnPreparePropertys 数据加载前，确保需要的属性被加载
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            //编码
            e.FieldKeys.Add("FBillNo");

            //数据来源
            e.FieldKeys.Add("F_DEV_DataSrc");

        }
        /// <summary>
        /// 事务结束后事件
        /// </summary>
        /// <param name="e"></param>
        public override void BeginOperationTransaction(BeginOperationTransactionArgs e)
        {
            base.BeginOperationTransaction(e);

            //读取全部的单据,for循环,转换成DynamicObject类型
            foreach (DynamicObject entity in e.DataEntitys)
            {
                string DataSrc = Convert.ToString(entity["F_DEV_DataSrc"]);//数据来源
                string FID = Convert.ToString(entity["Id"]);//内码                      
                string Number = entity["BillNo"].ToString();//编码
                if (DataSrc == "CRM")
                {
                    string LK = string.Format(@"/*dialect*/  select FSBILLID,FSID from T_CN_BILLRECEIVABLE_LK where FRULEID='RDLV_ClaimListToBillReceivableNew' 
and FSTABLENAME='RDLV_t_PendingCollection' and FID='{0}'", FID);
                    DataTable dtLK = DBServiceHelper.ExecuteDataSet(this.Context, LK).Tables[0];
                    if (dtLK.Rows.Count > 0)
                    {
                        foreach (DataRow item in dtLK.Rows)
                        {
                            string PeID = Convert.ToString(item["FSBILLID"]);//待认领收款FID
                            string UpPe = "";//更新sql
                            if (this.FormOperation.Operation.ToUpper() == "AUDIT")//审核更新待认领的已认领状态为true
                            {
                                //更新待认领收款清单的认领状态
                                UpPe = string.Format(@"/*dialect*/  update RDLV_t_PendingCollection set F_QLTK_Claimed='1' where FID='{0}'", PeID);
                                DBServiceHelper.Execute(this.Context, UpPe);
                            }
                            //else if (this.FormOperation.Operation.ToUpper() == "DELETE")//审核更新待认领的已认领状态为false
                            //{
                            //    //更新待认领收款清单的认领状态
                            //    UpPe = string.Format(@"/*dialect*/  update RDLV_t_PendingCollection set F_QLTK_Claimed='0' where FID='{0}'", PeID);
                            //    DBServiceHelper.Execute(this.Context, UpPe);
                            //}
                        }

                    }

                }
            }
        }
    }
}
